Seaborn & Plotly Visulaization Library Documentation¶

Library Overview¶

Seaborn¶

Seaborn is a Python Visualization library based on Matplotlib. It provides a high-level interface for creative statistical graphics. Seaborn simplifies the process of generating complex visualizations and is particularly useful for creating aesthetically pleasing plots with minimal code.

Typical use cases for Seaborn include data visualization in statistical analysis, machine learning, and data exploration tasks.

Plotly¶

Plotly is a Python graphing library that makes interactive, publication-quality graphs online. It supports a wide range of chart types and is known for its interactivity and ease of use. Plotly allows for creating interactive dashboards and web-based visualizations.

Plotly is commonly used for data visualization in web applications, interactive reports, and data-driven presentations.

Line Plot:¶

Seaborn:¶

• Strengths: Easy syntax for basic line plots, good for quick exploratory data analysis.

• Weaknesses: Limited interactivity, less suitable for highly dynamic or real-time data.

In [1]:
#seaborn - line plot

import seaborn as sns
import matplotlib.pyplot as plt

x = [ 1,2,3,4,5 ]
y = [ 10,15,7,20,12 ]

sns.lineplot(x = x,y = y)
plt.show()

Plotly:¶

• Strengths: High interactivity with hover effects, zooming, panning, and annotations.

• Weaknesses: Slightly more complex syntax for beginners, may require additional customization for aesthetics.

In [2]:
#plotly - line plot

import plotly.graph_objects as go

x = [ 1,2,3,4,5 ]
y = [ 10,15,7,20,12 ]

fig = go.Figure(data = go.Scatter(x=x,  y=y, mode = 'lines'))
fig.update_layout(width=700, height=500)
fig.show()

Scatter Plot:¶

Seaborn:¶

• Strengths: Simple syntax for basic scatter plots, integrates well with statistical analysis.

• Weaknesses: Limited interactivity, may require additional libraries for advanced customization.

In [3]:
#Seaborn - scatter plot

import seaborn as sns
import matplotlib.pyplot as plt

x = [ 1,2,3,4,5 ]
y = [ 10,15,7,20,12 ]

sns.scatterplot(x=x, y=y)
plt.show()

Plotly:¶

• Strengths: Excellent interactivity with tooltips, trend lines, and customizable markers.

• Weaknesses: Steeper learning curve, especially for beginners, due to its interactive nature.

In [4]:
#Plotly - scatter plot

import plotly.graph_objects as go

x = [ 1,2,3,4,5 ]
y = [ 10,15,7,20,12 ]

fig = go.Figure(data = go.Scatter( x=x, y=y, mode = 'markers'))
fig.update_layout(width=700, height=500)
fig.show()

Bar Chart:¶

Seaborn:¶

• Strengths: Easy syntax for basic bar charts, suitable for categorical data visualization.

• Weaknesses: Limited interactivity, may not be ideal for complex dashboards or web applications.

In [5]:
#Seaborn - bar chart

import seaborn as sns
import matplotlib.pyplot as plt

categories = [ 'A','B','C','D' ]
values = [ 10,15,7,20 ]

sns.barplot( x = categories, y = values)
plt.show()

Plotly:¶

• Strengths: Highly interactive bar charts with hover effects, drill-down capabilities, and animations.

• Weaknesses: Requires additional configuration for static outputs, may be overkill for simple bar charts.

In [6]:
#Plotly - bar chart

import plotly.graph_objects as go

categories = [ 'A','B','C','D' ]
values = [ 10,15,7,20 ]

fig = go.Figure(data = go.Bar( x = categories, y= values))
fig.update_layout(width=700, height=500)
fig.show()

Histogram:¶

Seaborn:¶

• Strengths: Simple syntax for creating histograms, integrates well with statistical analysis.

• Weaknesses: Limited interactivity, less suitable for dynamic data exploration.

In [7]:
#Seaborn - histogram

import seaborn as sns
import matplotlib.pyplot as plt

data = [ 1,2,2,3,3,3,3,3,4,4,4,5,5,6]

sns.histplot(data, bins = 6)
plt.show()

Plotly:¶

• Strengths: Interactive histograms with bin selection, hover effects, and density estimation.

• Weaknesses: Requires understanding of binning and density concepts, may be overwhelming for beginners.

In [8]:
#Plotly - histogram

import plotly.figure_factory as ff

data = [ 1,2,2,3,3,3,3,3,4,4,4,5,5,6]

fig = ff.create_distplot([data], group_labels = ['Histogram'] , bin_size = 1)
fig.update_layout(width=700, height=600)
fig.show()

Pie Chart:¶

Seaborn:¶

• Strengths: Limited support for pie charts, suitable for basic categorical distribution visualization.

• Weaknesses: Less flexible compared to other libraries, limited customization options.

In [9]:
#seaborn - Pie Chart
#Seaborn dosen't directly supports pie charts, so we use matplotlib for this

import matplotlib.pyplot as plt

sizes = [20, 30, 15, 35]
labels = ['A', 'B', 'C', 'D']

plt.pie(sizes, labels = labels, autopct = '%1.1f%%')
plt.show()

Plotly:¶

• Strengths: Highly customizable pie charts with interactive slices, labels, and colors.

• Weaknesses: Pie charts can be misleading, requires caution in usage for data representation.

In [10]:
#plotly - pie chart

import plotly.graph_objects as go

values = [20, 30, 15, 35]
labels = ['A', 'B', 'C', 'D']

fig = go.Figure(data = go.Pie(labels = labels, values = values))
fig.update_layout(width=500, height=600)
fig.show()

Box Plot:¶

Seaborn:¶

• Strengths: Easy syntax for creating box plots, integrates well with statistical analysis.

• Weaknesses: Limited interactivity, may require additional libraries for advanced customization.

In [11]:
#Seaborn - Box Plot

import seaborn as sns
import matplotlib.pyplot as plt

data = [ 1,2,2,3,3,3,3,3,4,4,4,5,5,6]

sns.boxplot(data)
plt.show()

Plotly:¶

• Strengths: Interactive box plots with hover effects, quartile selection, and outlier highlighting.

• Weaknesses: Requires understanding of quartiles and outliers, may be complex for beginners.

In [12]:
#plotly - box plot

import plotly.graph_objects as go

data = [ 1,2,2,3,3,3,3,3,4,4,4,5,5,6]

fig = go.Figure(data=go.Box(y=data))
fig.update_layout(width=600, height=500)
fig.show()

Violin Plot:¶

Seaborn:¶

• Strengths: Simple syntax for creating violin plots, suitable for visualizing distributions.

• Weaknesses: Limited interactivity, less suitable for dynamic data exploration.

In [13]:
#Seaborn - Violin Plot

import seaborn as sns
import matplotlib.pyplot as plt

data = [ 1,2,2,3,3,3,3,3,4,4,4,5,5,6]

sns.violinplot(data)
plt.show()

Plotly:¶

• Strengths: Interactive violin plots with hover effects, quartile selection, and kernel density estimation.

• Weaknesses: Requires understanding of kernel density estimation, may be challenging for beginners.

In [14]:
#plotly - Violin Plot

import plotly.graph_objects as go

data = [ 1,2,2,3,3,3,3,3,4,4,4,5,5,6]

fig = go.Figure(data = go.Violin(y= data))
fig.show()

Heatmap:¶

Seaborn:¶

• Strengths: Easy syntax for creating heatmaps, suitable for visualizing correlations and matrices.

• Weaknesses: Limited interactivity, may require additional libraries for advanced customization.

In [15]:
#Seaborn - Heatmap

import seaborn as sns
import matplotlib.pyplot as plt

flights = sns.load_dataset('flights')

flights_pivot = flights.pivot_table(index='month', columns='year', values='passengers')

sns.heatmap(flights_pivot, cmap='coolwarm', annot=True, fmt='d')
plt.show()

Plotly:¶

• Strengths: Interactive heatmaps with hover effects, annotations, and color scales.

• Weaknesses: Requires understanding of color scales and annotations, may be complex for beginners.

In [16]:
#Plotly - Heatmap

import plotly.express as px
import seaborn as sns

flights = sns.load_dataset('flights')
flights_pivot = flights.pivot_table(index='month', columns='year', values='passengers')

fig = px.imshow(
    flights_pivot,
    color_continuous_scale='YlGnBu',
    labels=dict(x="Year", y="Month", color="Passengers"),
    title='Passenger Counts by Month and Year'
)

fig.update_layout(width=800, height=600)
fig.show()

Line Plot with Error Bars:¶

Seaborn:¶

• Strengths: Supports error bars in line plots, integrates well with statistical analysis.

• Weaknesses: Limited interactivity, less suitable for real-time error analysis.

In [17]:
#Seaborn dosen't directly support error barss in line plots; we may use Matplotlib for this

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 15, 7, 20, 12]
y_error = [1, 2, 1.5, 2, 1]

plt.errorbar(x,y, yerr = y_error, fmt = '-o')
plt.show()

Plotly:¶

• Strengths: Interactive error bars in line plots with hover effects, customizable error bar styles.

• Weaknesses: Requires understanding of error bar concepts, may be overwhelming for beginners

In [18]:
#Plotly

import plotly.graph_objects as go

x = [1, 2, 3, 4, 5]
y = [10, 15, 7, 20, 12]
y_error = [1, 2, 1.5, 2, 1]

fig = go.Figure(data= go.Scatter(x=x , y=y, error_y = dict(type = 'data', array = y_error), mode = 'lines+markers'))
fig.update_layout(width=700, height=600)
fig.show()

3D Plotting:¶

Seaborn:¶

• Strengths: Limited support for 3D plotting, suitable for basic 3D visualizations.

• Weaknesses: Less flexible compared to dedicated 3D plotting libraries, limited customization.

In [19]:
#Seaborn dosen't directly support error barss in line plots; we may use Matplotlib for this

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
z = [1, 8, 27, 64, 125]

fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.scatter(x, y, z)
plt.show()

Plotly:¶

• Strengths: Excellent support for 3D plotting with interactive rotations, zooming, and surface plots.

• Weaknesses: Requires understanding of 3D coordinate systems, may be complex for beginners.

In [20]:
#plotly - 3d

import plotly.graph_objects as go

x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
z = [1, 8, 27, 64, 125]

fig = go.Figure(data= [go.Scatter3d( x=x , y=y, z=z, mode = 'markers')])
fig.update_layout(width=700, height=600)
fig.show()

Unique Charts in Seaborn:¶

1. Pair Plot:¶

Description:¶

• A pair plot is a grid of pairwise plots that show the relationships between variables in a dataset. It displays scatter plots for numerical variables and histograms for distributions on the diagonal.

Use Case:¶

• Exploring relationships between multiple variables simultaneously.

In [21]:
import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset('iris')
sns.pairplot(iris, hue = 'species')
plt.show()
C:\Users\valan\anaconda3\Lib\site-packages\seaborn\axisgrid.py:118: UserWarning:

The figure layout has changed to tight

2. Joint Plot:¶

Description:¶

• A joint plot combines scatter plots and histograms to visualize the joint distribution of two variables along with their marginal distributions.

Use Case:¶

• Analyzing the correlation and distribution between two variables.

In [22]:
import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset('tips')
sns.jointplot( x = 'total_bill', y = 'tip' , data = tips, kind = 'reg')
plt.show()

3. Cluster Map:¶

Description:¶

• A cluster map (heatmap) visualizes hierarchical clustering of rows and columns in a dataset, displaying similarities between data points.

Use Case:¶

• Identifying patterns and clusters in data, especially in correlation matrices.

In [23]:
import seaborn as sns
import matplotlib.pyplot as plt

flights = sns.load_dataset('flights')
flights_pivot = flights.pivot_table(index='month', columns='year', values='passengers')

sns.clustermap(flights_pivot, cmap = 'coolwarm', standard_scale = 1)
plt.show()

Unique Charts in Plotly:¶

1. Sunburst Chart:¶
Description:¶

• A sunburst chart is a hierarchical chart that displays hierarchical data as a set of nested rings. Each ring represents a level in the hierarchy, with segments showing the proportion of each category.

Use Case:¶

• Visualizing hierarchical data structures, such as organizational hierarchies or nested categories.

In [24]:
import plotly.express as px

df = px.data.tips()
fig = px.sunburst(df, path = ['sex', 'day', 'time'], values = 'total_bill')
fig.update_layout(width=700, height=600)
fig.show()

2. Sankey Diagram:¶

Description:¶

• A Sankey diagram is a flow diagram that visualizes the flow of data or resources between nodes, with the width of the arrows representing the quantity of flow.

Use Case:¶

• Illustrating flows in processes, such as energy flows, migration patterns, or sales funnels.

In [25]:
import plotly.graph_objects as go
fig = go.Figure(go.Sankey(
    node = dict(
        pad = 15,
        thickness = 20,
        line = dict(color = 'black', width = 0.5),
        label=["A", "B", "C", "D", "E"],
    ),
    link=dict(
        source=[0, 1, 1, 0, 2],
        target=[2, 3, 4, 4, 3],
        value=[8, 4, 2, 8, 4],
    ),
))
fig.show()
    

3. Choropleth Map:¶

Description:¶

• A choropleth map is a thematic map where areas are shaded or patterned in proportion to the value of a variable, such as population density or GDP per capita.

Use Case:¶

• Visualizing spatial data and geographical distributions, such as demographic trends or regional statistics.

In [28]:
import plotly.express as px
df = px.data.gapminder().query("year == 2007")
fig = px.choropleth(df, locations="iso_alpha", color="gdpPercap",
                     hover_name="country", color_continuous_scale=px.colors.sequential.Plasma)
fig.update_layout(width=800, height=700)
fig.show()

Conclusion:¶

Seaborn:¶

Strengths:¶

Simplicity, integration with statistical analysis, and ease of use for basic plots.

Weaknesses:¶

Limited interactivity and customization for complex visualizations.

Plotly:¶

Strengths:¶

Interactivity, customization, and support for advanced visualizations like 3D plots.

Weaknesses:¶

Steeper learning curve, complexity for beginners, and potential overkill for simple plots.